package com.curluin.model;

import android.util.Log;

import com.curluin.controll.Controller;
import com.curluin.math.MyVector;
import com.curluin.scene.Cube;
import com.curluin.scene.Node;
import com.curluin.scene.TextureNode;
import com.curluin.scene.TriangleMesh;

public class Goal extends Ground {

	private static final String LOG_TAG = "Goal";
	
	private static final float maxScore=99.99f;

	private Node targetNode; 
	
	/**
	 * Constructor
	 */
	public Goal(float friction,MyVector position,int positionMode,float b,float h,float l, MyVector color, MyVector targetPositionProportional) {
		super(friction,Controller.GAMEPHASE_FINISH, position, positionMode, b, h, l, color, ModelObject.CONSITENCY_HARD_AS_ICE);
		MyVector size = MyVector.makeVector3(b, h, -l);
		if(this.getClass().equals(Goal.class)){ 
			initialize(position, positionMode);	
			MyVector targetPosition = node.getCenter().subtract(size.multiply(0.5f)).add(size.multiplyElementwise(targetPositionProportional));
			targetNode = new Node(targetPosition);
			targetNode.addChild(new Cube(MyVector.makeVector3(0, 0, 0), 5, 10, 5, TriangleMesh.GREEN));
			targetNode.updateOutersizeAndCenter();
		}
		Log.e(LOG_TAG, "Goal Pos: "+node.getCenter()+" Size "+node.outerSize);
		Log.e(LOG_TAG, "Target Pos: "+targetNode.getCenter()+" Size "+targetNode.outerSize);
	}
	
	public Goal(float friction,MyVector position,int positionMode,float b,float h,float l, final int textureRinkResourceId, MyVector targetPositionProportional, final int textureGoalResourceId) {
		//this(friction, position, positionMode, b, h, l, TriangleMesh.RED, targetPositionProportional);
			
		super(friction,Controller.GAMEPHASE_FINISH, position, positionMode, b, h, l, TriangleMesh.RED, ModelObject.CONSITENCY_HARD_AS_ICE);
		MyVector size = MyVector.makeVector3(b, h, -l);
		if(this.getClass().equals(Goal.class)){ 
			initialize(position, positionMode);	
			MyVector targetPosition = node.getCenter().subtract(size.multiply(0.5f)).add(size.multiplyElementwise(targetPositionProportional));
			targetNode = new Node(targetPosition);
			TextureNode texNodeGoal = new TextureNode(textureGoalResourceId);
			texNodeGoal.addChild(new Cube(MyVector.makeVector3(0, 0.1f, 0), 23, 0.3f, 20, TriangleMesh.GREEN));
			targetNode.addChild(texNodeGoal);
			targetNode.updateOutersizeAndCenter();
		}
		
		TextureNode texNodeRink = new TextureNode(textureRinkResourceId);
	    texNodeRink.addChild(this.node.getChildren().firstElement());
		this.node.getChildren().clear();
		this.node.addChild(texNodeRink);
	}
	
	public Goal(float friction,MyVector position,int positionMode,float[] textureSize, int[] numTextPerInt , final int textureRinkResourceId, MyVector targetPositionProportional, TriangleMesh targetMesh) {
		//this(friction, position, positionMode, b, h, l, TriangleMesh.RED, targetPositionProportional);
			
		super(friction,Controller.GAMEPHASE_FINISH, position, positionMode, textureSize, numTextPerInt, textureRinkResourceId, ModelObject.CONSITENCY_HARD_AS_ICE);
		MyVector size = MyVector.makeVector3(textureSize[0]*numTextPerInt[0], textureSize[1]*numTextPerInt[1], textureSize[2]*numTextPerInt[2]);
		if(this.getClass().equals(Goal.class)){ 
			initialize(position, positionMode);	
			MyVector targetPosition = node.getCenter().subtract(size.multiply(0.5f)).add(size.multiplyElementwise(targetPositionProportional));
			targetNode = new Node(targetPosition);
			targetNode.addChild(targetMesh);
			targetNode.updateOutersizeAndCenter();
			targetNode.setPosition(MyVector.makeVector3(0, targetNode.outerSize.get(1)/2, 0), true, Node.CENTER);
			debugColor=TriangleMesh.ICE_BLUE;
		}
		
		TextureNode texNodeRink = new TextureNode(textureRinkResourceId);
	    texNodeRink.addChild(this.node.getChildren().firstElement());
		this.node.getChildren().clear();
		this.node.addChild(texNodeRink);
		
	}

	@Override
	protected Node composeGraphics(){
		Node n = super.composeGraphics();

		return n;
	}

	/**computes the score by using the distance from Pinguin to Goal/target
	 * @param pinguinPosition 
	 * @return score dependend on distance to target
	 */
	public int computeScore(MyVector pinguinPosition){
		//TODO maybe better evaluation-function
		float distance=distanceToTarget(pinguinPosition);

		float maxDist = node.outerSize.get(2);//get points even if pinguin is not in gamephase finish
		int score=(int) (maxScore*(1-distance/maxDist));
		score = Math.max(0, score);
		return score;
	}

	public Node getTargetNode(){
		return targetNode;
	}

	public float distanceToTarget(MyVector pos){
		return getTargetPosition().subtract(pos).getNorm();
	}

	public MyVector getTargetPosition(){
		return targetNode.position;
	}


}
